/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：ConsumeLogDaoImpl.java
 * History:
 *       May 14, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.dao.Impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xteam.ecsystem.dao.IConsumeLogDao;
import com.xteam.ecsystem.entities.BaseMemberBase;
import com.xteam.ecsystem.entities.LogConsumeLog;
import com.xteam.ecsystem.utils.JsonDateValueProcessor;
import com.xteam.ecsystem.utils.PageCenterStation;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 消费日志数据访问实现类
 * @author liuzhi
 *
 */
@Repository
@SuppressWarnings("unchecked")
public class ConsumeLogDaoImpl extends DDLPrefix implements IConsumeLogDao {
	
	@Autowired
	private BaseDaoImpl baseDao;

	public void delConsumeLogsByIds(String ids) {
		try {
			List list = new ArrayList();
			String oid[] = ids.split(",");
			
			for (String id : oid) {
				Object obj = this.baseDao.get(LogConsumeLog.class, id);
				if (obj != null) {
					list.add(obj);
				}
			}
			
			this.baseDao.deleteAll(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getConsumeLogList(int pageIndex, int pageSize, String bmbNick, 
			String startTime, String endTime) {
		JSONObject jo = new JSONObject();
		String hql = "";
		String countHQL = "";
		
		try {
			List list = new ArrayList();
			
			hql = SQL_SELECT + "new " + LogConsumeLog.REF + " (lcl.lclId, lcl.lclMember, lcl.lclOrigin, " 
				+ "lcl.lclCurrent, lcl.lclAmount, lcl.lclEffect, lcl.lclRemark, bmb.bmbNick)" 
				+ SQL_FROM + LogConsumeLog.REF + " as lcl," + BaseMemberBase.REF + " as bmb" 
				+ SQL_WHERE + "lcl.lclMember = bmb.bmbId ";
			
			countHQL = SQL_SELECT + " count(*) " + SQL_FROM + LogConsumeLog.REF + " as lcl, " 
					 + BaseMemberBase.REF + " as bmb" + SQL_WHERE + "lcl.lclMember = bmb.bmbId ";
			
			if (StringUtil.isNotNullOrEmpty(bmbNick)) {
				hql += SQL_AND + " bmb.bmbNick " + SQL_LIKE + " ? ";
				countHQL += SQL_AND + " bmb.bmbNick " + SQL_LIKE + " ? ";
				list.add("%" + bmbNick + "%");

			}
						
			if (StringUtil.isNotNullOrEmpty(startTime)) {
				hql += " and lcl.lclEffect >=? "; 
				countHQL += " and lcl.lclEffect >=? ";
			    list.add(StringUtil.strToDate(startTime, null));
			 }
			
			if (StringUtil.isNotNullOrEmpty(endTime)) {
				hql += " and lcl.lclEffect <=? "; 
				countHQL += " and lcl.lclEffect <=? ";
			    list.add(StringUtil.strToDate(endTime, null));
			 }
			
			hql += SQL_ORDERBY + "lcl.lclEffect desc";

			PageCenterStation<?> p = (PageCenterStation<?>) this.baseDao
					.queryList(countHQL, hql, list.toArray(), 
							pageIndex * pageSize, pageSize);
	
			// 率先格式化日期，在取数据时便不必再作处理。
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(Date.class,
					new JsonDateValueProcessor("yyyy-MM-dd HH:mm:ss"));
			
			jo = JSONObject.fromObject(p, jsonConfig);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return jo.toString();
	}
	
	public void delConsumeLogByCon(String condition){
		String hql = "";
		
		try {
			if (StringUtil.isNotNullOrEmpty(condition)) {
				int type = Integer.parseInt(condition);
				
				switch (type) {
				case 0: // 一周之前
					hql = SQL_DELETE + SQL_FROM + LogConsumeLog.REF + SQL_WHERE 
							+ "DATEDIFF(NOW(), lclEffect) > 7";  
					break;
				case 1: // 一个月之前
					hql = SQL_DELETE + SQL_FROM + LogConsumeLog.REF + SQL_WHERE 
					+ "DATEDIFF(NOW(), lclEffect) > 30"; 
					break;
				case 2: // 三个月之前
					hql = SQL_DELETE + SQL_FROM + LogConsumeLog.REF + SQL_WHERE 
					+ "DATEDIFF(NOW(), lclEffect) > 90"; 
					break;
				case 3: // 半年之前
					hql = SQL_DELETE + SQL_FROM + LogConsumeLog.REF + SQL_WHERE 
					+ "DATEDIFF(NOW(), lclEffect) > 180"; 
					break;
				case 4: // 一年之前
					hql = SQL_DELETE + SQL_FROM + LogConsumeLog.REF + SQL_WHERE 
					+ "DATEDIFF(NOW(), lclEffect) > 360"; 
					break;
				default:
					break;
				}
				
				this.baseDao.executeUpdate(hql);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
